BEGIN;
	CREATE FUNCTION mark_offers_when_accepterd() RETURNS trigger AS $$
	BEGIN
		IF ('UPDATE'=TG_OP AND NOT(OLD.item_owner_agreed AND OLD.requester_agreed) AND NEW.item_owner_agreed AND NEW.requester_agreed)
			OR ('INSERT'=TG_OP AND NEW.item_owner_agreed AND NEW.requester_agreed) THEN
			UPDATE offers SET another_offer_accepted=true WHERE request_id=NEW.request_id AND id!=NEW.id;
		END IF;
		RETURN NEW;
	END;
	$$ LANGUAGE plpgsql;

	CREATE TRIGGER trigger_mark_offers_when_accepterd AFTER INSERT OR UPDATE ON offers FOR EACH ROW EXECUTE PROCEDURE mark_offers_when_accepterd();
END;
